{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 完成数组的添加练习",
   "id": "a9657704f6b19b04"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-02-26T15:46:18.246888Z",
     "start_time": "2025-02-26T15:46:18.201607Z"
    }
   },
   "source": "import numpy as np",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T15:49:18.287500Z",
     "start_time": "2025-02-26T15:49:18.284409Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.arange(24).reshape(4, 6)\n",
    "print(a)"
   ],
   "id": "f8bdf5795087df2d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T15:56:45.282381Z",
     "start_time": "2025-02-26T15:56:45.278764Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# numpy.append()\n",
    "b = np.append(a, np.arange(6).reshape(1, 6), axis=0)\n",
    "print(b, b.shape)\n",
    "print('-' * 50)\n",
    "c = np.append(a, np.arange(4).reshape(4, 1), axis=1)\n",
    "print(c, c.shape)\n",
    "print('-' * 50)\n",
    "# 不指定轴，就默认全部展开，然后添加到末尾\n",
    "d = np.append(a, [1, 2, 3, 4, 5, 6])\n",
    "print(d, d.shape)"
   ],
   "id": "cbb1c5ac45e13716",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]\n",
      " [ 0  1  2  3  4  5]] (5, 6)\n",
      "--------------------------------------------------\n",
      "[[ 0  1  2  3  4  5  0]\n",
      " [ 6  7  8  9 10 11  1]\n",
      " [12 13 14 15 16 17  2]\n",
      " [18 19 20 21 22 23  3]] (4, 7)\n",
      "--------------------------------------------------\n",
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23\n",
      "  1  2  3  4  5  6] (30,)\n"
     ]
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T16:04:55.494443Z",
     "start_time": "2025-02-26T16:04:55.491235Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# numpy.insert()\n",
    "a = np.arange(24).reshape(4, 6)\n",
    "print(a, a.shape)\n",
    "print('-' * 50)\n",
    "# insert不指定轴，就默认全部展开，然后插入到指定位置\n",
    "print(np.insert(a, 3, [20, 21, 22, 23, 24, 25]))\n",
    "print('-' * 50)\n",
    "print(np.insert(a, 1, [11, 22, 33, 44, 55, 66], axis=0))\n",
    "print('-' * 50)\n",
    "print(np.insert(a, 1, [11, 22, 33, 44], axis=1))\n",
    "print('-' * 50)"
   ],
   "id": "6a883012123ad02c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]] (4, 6)\n",
      "--------------------------------------------------\n",
      "[ 0  1  2 20 21 22 23 24 25  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17\n",
      " 18 19 20 21 22 23]\n",
      "--------------------------------------------------\n",
      "[[ 0  1  2  3  4  5]\n",
      " [11 22 33 44 55 66]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "--------------------------------------------------\n",
      "[[ 0 11  1  2  3  4  5]\n",
      " [ 6 22  7  8  9 10 11]\n",
      " [12 33 13 14 15 16 17]\n",
      " [18 44 19 20 21 22 23]]\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 56
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T16:03:08.718343Z",
     "start_time": "2025-02-26T16:03:08.715359Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.arange(24).reshape(4, 6)\n",
    "print(a, a.shape)"
   ],
   "id": "3b5e008fcdb49732",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]] (4, 6)\n"
     ]
    }
   ],
   "execution_count": 52
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T16:05:20.920877Z",
     "start_time": "2025-02-26T16:05:20.918519Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.array([[1, 2], [3, 4], [5, 6]])\n",
    "print(a)"
   ],
   "id": "faac84243a1c367a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n"
     ]
    }
   ],
   "execution_count": 57
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T16:07:39.354353Z",
     "start_time": "2025-02-26T16:07:39.351964Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# print(np.insert(a, 3, [11, 12]))\n",
    "print(np.insert(a, 1, [11, 12], axis=0))\n",
    "print(np.insert(a, 1, [11, 12, 13], axis=1))\n",
    "# print(np.insert(a, 1, [11], axis=0))\n",
    "# print(np.insert(a, 1, 11, axis=1))"
   ],
   "id": "577eb1a45f10ad18",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2]\n",
      " [11 12]\n",
      " [ 3  4]\n",
      " [ 5  6]]\n",
      "[[ 1 11  2]\n",
      " [ 3 12  4]\n",
      " [ 5 13  6]]\n"
     ]
    }
   ],
   "execution_count": 67
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
